# -*- coding: utf-8 -*-

from pydantic import BaseModel, EmailStr

from fastapi import HTTPException
from itsdangerous import URLSafeTimedSerializer, SignatureExpired, BadSignature

from app.config import settings

serializer = URLSafeTimedSerializer(settings.SECRET_KEY)

class VerifyEmailCode(BaseModel):
    email: EmailStr
    code: str

async def generate_link(email: EmailStr):
    token = serializer.dumps({'email': email}, salt="activate")
    # return f"{settings.FRONTEND_URL}/activate/{token}"
    return token

async def verify_link(token: str):
    try:
        data = serializer.loads(token, salt="activate", max_age=300)
    except SignatureExpired:
        raise HTTPException(status_code=400, detail="链接已过期")
    except BadSignature:
        raise HTTPException(status_code=400, detail="链接无效")
    # TODO: 激活账号
    return data